﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class ListeYetkiliDepolar : DevExpress.XtraEditors.XtraForm
    {
        public int User_Id=0;

        public ListeYetkiliDepolar()
        {
            InitializeComponent();
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void _btnKullaniciListesi_Click(object sender, EventArgs e)
        {

            ListeDepolar dl = new ListeDepolar();
            dl.ForSelection = true;
            dl.ShowDialog();

            if (Army.EventConfirm == true)
            {
                _txtDepoSecInt.Text = Army.SecilenID.ToString();
                DataTable dt = Army.SQL().GetDataTable("Select id,DepoAdi from Depolar where id=" + _txtDepoSecInt.Text);

                foreach (DataRow item in dt.Rows)
                {

                    foreach (ListViewItem lt in _lwDepolarListe.Items)
                    {
                        if (lt.SubItems[1].Text == _txtDepoSecInt.Text)
                        {
                            MessageBox.Show("Eklemek istediğiniz depo zaten ekli.\nAynı depoyu tekrar ekleyemezsiniz.", "Tekrarlanan Depo!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    //ListViewItem ls = new ListViewItem(item[0].ToString());
                    //ls.SubItems.Add(item[1].ToString());

                    //_lwDepolarListe.Items.Add(ls);

                    Army.SQL().cmd("INSERT INTO KullaniciDepo(id_kullanici,id_depolar) VALUES(" + User_Id + "," + item[0].ToString() + ")");
                    ListeGuncelle();
                }
            }

            _txtDepoSecInt.Text = "";
        }

        public void ListeGuncelle() {
            _lwDepolarListe.Items.Clear();
            string Sorgu = "Select \n" +
                "KullaniciDepo.id ID, \n" +
                "Depolar.DepoAdi [Depo Adı], \n" +
                "KullaniciDepo.id_depolar [DepoId] \n" +
                "FROM \n" +
                "KullaniciDepo, Kullanici, Depolar \n" +
                "WHERE \n" +
                "KullaniciDepo.id_kullanici=Kullanici.kID \n" +
                "AND KullaniciDepo.id_depolar = Depolar.id \n" +
                "AND KullaniciDepo.id_kullanici=" + User_Id;

            DataTable dt = Army.SQL().GetDataTable(Sorgu);

            foreach (DataRow item in dt.Rows)
            {
                ListViewItem ls = new ListViewItem(item["ID"].ToString());
                ls.SubItems.Add(item["DepoId"].ToString());
                ls.SubItems.Add(item["Depo Adı"].ToString());

                _lwDepolarListe.Items.Add(ls);
            }

        }

        private void ListeYetkiliDepolar_Load(object sender, EventArgs e)
        {
            ListeGuncelle();
        }

        public void Sil()
        {

            if (_lwDepolarListe.SelectedItems.Count > 0)
            {
                Army.SQL().cmd("Delete from KullaniciDepo where id=" + _lwDepolarListe.SelectedItems[0].SubItems[0].Text.ToString());
            }
            ListeGuncelle();

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Sil();
        }

        private void silToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Sil();
        }
    }
}